/* File Name: getKey.h
 * Author: Guo Song
 * Date: 19/01/2008
 * Update:
 * Description: there is only one function which is meant to get key 
 *		from KDC server. Straight forward it is.
 *		You create a connection to the KDC server, you accept 
 *		the string pushed from the KDC server.
 *              Then the connection is closed by KDC.
 */

#ifndef _GETKEY_H_
#define _GETKEY_H_

#include "4225_6225.h"
#define BUFFSIZE 20
#define DEFUALT_PORT 32000
char KDC_SERV_IP[] = "131.96.49.118";

char* getKey(char addr[], int KDC_port)//, int my_port)
{
    int sockfd;
    struct sockaddr_in servaddr;          /* connection socket address */
    int int_key;                              /* store the key sent from KDC */
    int n = 0;
    sockfd = Socket(AF_INET, SOCK_STREAM, 0);     /* create socket */

    bzero(&servaddr, sizeof(servaddr));           /* init the address */
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(KDC_port);
    inet_aton(addr, &servaddr.sin_addr);

    Connect(sockfd, (SA *) &servaddr, sizeof(servaddr));  /* connect to server */
    /* read key from KDC which is an integer */
    n = Readn(sockfd, &int_key, sizeof(int));
    Close(sockfd);
    if(n > 0)
    {
	char* ch_key = (char* )malloc(sizeof(char) * BUFFSIZE);
	memset(ch_key, '\0', BUFFSIZE);
	sprintf(ch_key, "%d", int_key);
	//itoa(int_key, ch_key, 10);
	return ch_key;
	//return int_key;
    }
    else
    {
	return NULL;
    }

}

#endif
